package com.dooioo.pns.base.model;

import com.alibaba.fastjson.annotation.JSONField;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.Alias;

import java.util.List;

/**
 * 分页类
 *
 * @author Jail Hu
 * @date 2014年11月12日
 */
@Alias("pagination")
public class Pagination<T> {
	/**
	 * 查询结果集
	 */
	private List<T> list;
	/**
	 * 查询 条件
	 */
	private String where;
	/**
	 * 排序 条件 ，无需输入 Order by
	 */
	private String order;
	/**
	 * 页号 第一页为1
	 */
	private int pageNo = 1;
	/**
	 * 页尺寸
	 */
	private int pageSize = 1;

	/**
	 * 总结果数
	 */
	private int totalRecords;

	/**
	 * 获得 list List<E>
	 *
	 * @return list
	 */
	public List<T> getList() {
		return list;
	}

	/**
	 * 设置 list
	 *
	 * @param list
	 */
	public void setList(List<T> list) {
		this.list = list;
	}

	/**
	 * 获得 where String
	 *
	 * @return where
	 */
	@JSONField(serialize = false)
	public String getWhere() {
		return where;
	}

	/**
	 * 设置 where
	 *
	 * @param where
	 */
	public void setWhere(String where) {
		this.where = where;
	}

	/**
	 * 获得 order String
	 *
	 * @return order
	 */
	@JSONField(serialize = false)
	public String getOrder() {
		return order;
	}

	/**
	 * 设置 order
	 *
	 * @param order
	 */
	public void setOrder(String order) {
		this.order = order;
	}

	/**
	 * 获得 pageNo int
	 *
	 * @return pageNo
	 */
	public int getPageNo() {
		return pageNo;
	}

	/**
	 * 设置 pageNo
	 *
	 * @param pageNo
	 */
	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	/**
	 * 获得 pageSize int
	 *
	 * @return pageSize
	 */
	public int getPageSize() {
		return pageSize;
	}

	/**
	 * 设置 pageSize
	 *
	 * @param pageSize
	 */
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	/**
	 * 获得总记录数
	 *
	 * @return int
	 */
	public int getTotalRecords() {
		return this.totalRecords;
	}

	/**
	 * 设置总记录数
	 *
	 * @param totalRecords
	 */
	public void setTotalRecords(int totalRecords) {
		this.totalRecords = totalRecords;
	}

	/**
	 * 获得 总页面数
	 *
	 * @return int
	 */
	public int getTotalPages() {
		return this.totalRecords % this.pageSize == 0 ? this.totalRecords
				/ this.pageSize : (this.totalRecords / this.pageSize) + 1;
	}

	/**
	 * 获得 where / order 的 sql 补充语句
	 *
	 * @return String
	 */
	@JSONField(serialize = false)
	public String getExtensionSql() {
		return (StringUtils.isBlank(this.getWhere()) ? "" : " where "
				+ this.getWhere())
				+ (StringUtils.isBlank(this.getOrder()) ? "" : " order by "
						+ this.getOrder());
	}

	/**
	 * 获得 limit 补充 sql
	 *
	 * @return String
	 */
	@JSONField(serialize = false)
	public String getLimitSql() {
		return " limit "
				+ ((this.getPageNo() < 1 ? 0 : this.getPageNo() - 1) * this
						.getPageSize()) + "," + this.getPageSize();
	}

	/**
	 * 获得 where 的Sql语句，包含 where 关键词
	 *
	 * @return
	 */
	@JSONField(serialize = false)
	public String getWhereSql() {
		return StringUtils.isBlank(this.getWhere()) ? "" : (" where " + this
				.getWhere());
	}

	/**
	 * Sql Server 使用！ 获得 row_number 的Sql 语句，包含 order By
	 *
	 * @return
	 */
	@JSONField(serialize = false)
	public String getRowNumberSql() {
		return " row_number() over(order by " + this.getOrder() + ") as rid ";
	}

	/**
	 * Sql Server 使用！ 获得 分页的sql 语句，包含 where
	 *
	 * @return
	 */
	@JSONField(serialize = false)
	public String getPageSql() {
		return " where rid between "
				+ ((this.getPageNo() < 1 ? 0 : this.getPageNo() - 1)
						* this.getPageSize() + 1) + " and "
				+ (this.getPageNo() < 1 ? 0 : this.getPageNo())
				* this.getPageSize();
	}

	@Override
	public String toString() {
		return "Pagination [list=" + list + ", where=" + where + ", order="
				+ order + ", pageNo=" + pageNo + ", pageSize=" + pageSize
				+ ", totalRecords=" + totalRecords + ", getTotalPages()="
				+ getTotalPages() + ", getExtensionSql()=" + getExtensionSql()
				+ ", getLimitSql()=" + getLimitSql() + "]";
	}
}
